package com.card.game.dn;

import java.util.Collections;
import java.util.Vector;

import com.card.game.Card;
import com.card.game.CardUtils;

/**
 * 算分规则：
三倍封顶，无牛到牛六一杯，牛789两倍，牛牛以上三倍
五倍封顶，无牛到牛六一倍，牛789两倍，牛牛三倍，葫芦，同花，顺子4倍，其他五倍
10倍封顶, 同花顺10倍，4条9倍，五花牛8倍，五小牛7倍，葫芦6倍，同花，顺子5倍，牛牛4倍，牛9三倍，牛七牛八2倍，无牛到牛六一倍

 * @author xiaoaogame
 *
 */
public class DzUtils {
	//同花顺>炸弹>五花牛>五小牛>葫芦>同花>顺子>牛牛>有牛>没牛
	//niuNum的意思  0-10 无牛-牛牛
	public static final int TongHuaShun = 160;//同花顺
	public static final int ZhanDan = 150;//炸弹
	public static final int WuHuaNiu = 140;//五花牛
	public static final int WuXiaoNiu = 130;//五小牛
	public static final int HuLu = 120;//葫芦
	public static final int TongHua = 110;//同花
	public static final int LianZi = 100;//顺子
	
	private static boolean haveNiu(Vector<Card> vecs) {
		for (int i = 0; i < vecs.size(); i++) {
			for (int j = i+1; j < vecs.size(); j++) {
				for (int k = j+1; k < vecs.size(); k++) {
					int n1 = vecs.get(i).getNiuIndex();
					int n2 = vecs.get(j).getNiuIndex();
					int n3 = vecs.get(k).getNiuIndex();
					if((n1+n2+n3)%10 == 0) {
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public static int getNiuNum(Vector<Card> sours) {
		Vector<Card> vecs = (Vector<Card>)sours.clone();
		Collections.sort(vecs);
		boolean isTongHua = isTongHua(vecs);
		boolean isLianZi = isLianZi(vecs);
		if(isLianZi && isTongHua) {
			return TongHuaShun;
		}
		if(is4Zha(vecs)) {
			return ZhanDan;
		}
		if(is5XiaoNiu(vecs)) {
			return WuXiaoNiu;
		}
		boolean haveNiu = haveNiu(vecs);
		if(haveNiu) {
			if(is5HuaNiu(vecs)) {
				return WuHuaNiu;
			}
		}
		if(isHuLu(vecs)) {
			return HuLu;
		}
		if(isTongHua) {
			return TongHua;
		}
		if(isLianZi) {
			return LianZi;
		}
		if(haveNiu) {
			int total = 0;
			for (Card card : vecs) {
				total += card.getNiuIndex();
			}
			int niu = total%10;
			if(niu == 0) {
				return 10;
			}
			return niu;
		}
		return 0;
	}
	
	public static boolean is5XiaoNiu(Vector<Card> vecs) {
		int num = 0;
		for (Card card : vecs) {
			if(card.getNiuIndex() > 5) {
				num += card.getNiuIndex();
				return false;
			}
		}
		return num<=10;
	}
	
	public static boolean isTongHua(Vector<Card> cards) {
		Card first = cards.get(0);
		for (int i = 1; i < cards.size(); i++) {
			if(cards.get(i).getType() != first.getType()) return false;
		}
		return true;
	}
	public static boolean isHuLu(Vector<Card> vecs) {
		int i0 = vecs.get(0).getIndex();
		int i1 = vecs.get(1).getIndex();
		int i2 = vecs.get(2).getIndex();
		int i3 = vecs.get(3).getIndex();
		int i4 = vecs.get(4).getIndex();
		return i0==i1 && i3==i4 && (i1 == i2 || i2 == i3) ;
	}
	
	public static boolean isLianZi(Vector<Card> vecs) {
		int i0 = vecs.get(0).getIndex();
		int i1 = vecs.get(1).getIndex();
		int i4 = vecs.get(4).getIndex();
		for (int i = 2; i < vecs.size(); i++) {
			if(i1+(i-1) != vecs.get(i).getIndex()) {
				return false;
			}
		}
		if(i0+1 == i1) {
			return true;
		}
		return false;
	}
	
	public static boolean is4Zha(Vector<Card> vecs) {
		int i0 = vecs.get(0).getIndex();
		int i1 = vecs.get(1).getIndex();
		int i2 = vecs.get(2).getIndex();
		int i3 = vecs.get(3).getIndex();
		int i4 = vecs.get(4).getIndex();
		return i0==i1 && i0==i2 && i0==i3 
				|| i4==i1 && i4==i2 && i4==i3 ;
	}
	public static boolean is5HuaNiu(Vector<Card> vecs) {
		for (Card card : vecs) {
			if(card.getIndex() <= 10) {
				return false;
			}
		}
		return true;
	}
	
	public static void main(String[] args) {
		Vector<Card> c1 = CardUtils.testCards("1-6;1-3;1-7;2-10;1-1;");
		Vector<Card> c2 = CardUtils.testCards("1-6;1-3;1-7;0-10;1-10;");
		NnInfo info1 = new NnInfo("1");
		NnInfo info2 = new NnInfo("2");
		int n1 = DzUtils.getNiuNum(c1);
		info1.setCards(c1, n1);
		int n2 = DzUtils.getNiuNum(c2);
		info2.setCards(c2, n2);
		System.err.println(n1+"/"+n2);
		System.err.println(info1.compareTo(info2)>0);
	}
}
